package com.fy.oa.zc.handler;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

import com.fy.oa.zc.service.FyOAFormEventDistribution;
import com.seeyon.apps.collaboration.event.AbstractCollaborationEvent;
import com.seeyon.apps.collaboration.event.CollaborationCancelEvent;
import com.seeyon.apps.collaboration.event.CollaborationProcessEvent;
import com.seeyon.apps.collaboration.event.CollaborationStartEvent;
import com.seeyon.apps.collaboration.event.CollaborationStepBackEvent;
import com.seeyon.apps.collaboration.event.CollaborationStopEvent;
import com.seeyon.ctp.event.EventTriggerMode;
import com.seeyon.ctp.util.annotation.ListenEvent;

/**
 * 事件监听
 * 
 * @author 郑志诚
 *
 */
public class FyOAFormEventHandler {
//	private static final Log logger = LogFactory.getLog(FyOAFormEventHandler.class);

//	private void pringLog(Object msg) {
//		logger.info(msg);
//		System.out.println(msg);
//	}

	/**
	 * 流程提交通过事件
	 *
	 * @param event
	 *            {@link CollaborationProcessEvent}
	 */
	@ListenEvent(event = CollaborationProcessEvent.class, mode = EventTriggerMode.afterCommit)
	public void onPress(CollaborationProcessEvent event) {
		distribute(event);
	}

	/**
	 * 流程结束通过事件
	 *
	 * @param event
	 *            {@link CollaborationFinishEvent}
	 */
	@ListenEvent(event = com.seeyon.apps.collaboration.event.CollaborationFinishEvent.class, mode = EventTriggerMode.afterCommit)
	public void onFinish(com.seeyon.apps.collaboration.event.CollaborationFinishEvent event) {
		distribute(event);
	}

	/**
	 * 发送表单事件
	 *
	 * @param event
	 *            {@link CollaborationStartEvent}
	 */
	@ListenEvent(event = CollaborationStartEvent.class, mode = EventTriggerMode.afterCommit)
	public void onFlowStart(CollaborationStartEvent event) {
		distribute(event);
	}

	/**
	 * 终止表单事件
	 *
	 * @param event
	 *            {@link CollaborationStopEvent}
	 */
	@ListenEvent(event = CollaborationStopEvent.class, mode = EventTriggerMode.afterCommit)
	public void onFlowStop(CollaborationStopEvent event) {
		distribute(event);
	}

	/**
	 * 撤销表单事件
	 *
	 * @param event
	 *            {@link CollaborationCancelEvent}
	 */
	@ListenEvent(event = CollaborationCancelEvent.class, mode = EventTriggerMode.afterCommit)
	public void onFlowCancel(CollaborationCancelEvent event) {
		distribute(event);
	}

	/**
	 * 表单回退事件
	 *
	 * @param event
	 *            {@link CollaborationStepBackEvent}
	 */
	@ListenEvent(event = CollaborationStepBackEvent.class, mode = EventTriggerMode.afterCommit)
	public void onFlowStepBack(CollaborationStepBackEvent event) {
		distribute(event);
	}

	/**
	 * 事件处理分发
	 * 
	 * @param event
	 *            {@link AbstractCollaborationEvent}
	 */
	private void distribute(AbstractCollaborationEvent event) {
			FyOAFormEventDistribution fyOAFormEventDistribution = new FyOAFormEventDistribution();
//			pringLog(fyOAFormEventDistribution.toString());
			fyOAFormEventDistribution.setEvent(event);
			Thread thread = new Thread(fyOAFormEventDistribution);
			thread.start();
	}
}
